import json
import os
import traceback
from time import sleep

import requests

file = []
dir = []
dir_res = []
give_path = '/Users/zh/Documents/查重'


def list_dir(start_dir):
    dir_res = os.listdir(start_dir)
    for path in dir_res:
        temp_path = start_dir + '/' + path
        if os.path.isfile(temp_path):
            file.append(temp_path)
        if os.path.isdir(temp_path):
            dir.append(temp_path)
            list_dir(temp_path)


def parseDoc(path):
    try:
        print(path)
        title = path.split('/Users/zh/Documents/查重/')[1]
        filename = path.replace('/', '//')
        res = requests.get(f'http://127.0.0.1:8080/v1/parse/docpath?filename={filename}')
        if res.status_code == 200:
            data = json.loads(res.text)
            content = data['content'].replace('Evaluation Warning: The document was created with Spire.Doc for JAVA.', '')
            headers = {
                "Content-Type": "application/json"
            }
            postData = {
                'title': title,
                'content': content,
                'author': ''
            }
            submit_res = requests.post('http://127.0.0.1:5000/v1/article/batch_submit', data=json.dumps(postData),
                                       headers=headers)
            print(submit_res.status_code)
    except Exception as e:
        print(e)
        print(path)


if __name__ == '__main__':
    list_dir(give_path)
    for index, path in enumerate(file):
        print(path)
        print(index)
        if index > 254:
            parseDoc(path)
            sleep(2)
    #
